package com.heima.studentinfoplus.controller;

import com.heima.studentinfoplus.entity.LoginRequest;
import com.heima.studentinfoplus.entity.LoginResponse;
import com.heima.studentinfoplus.entity.User;
import com.heima.studentinfoplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册接口
     */

    @PostMapping("/register")
    public Object register(@RequestBody User user) {
        try {
            User registeredUser = userService.register(user);
            return new ResponseResult(0, "注册成功");
        } catch (Exception e) {
            return new ResponseResult(-1, e.getMessage());
        }
    }


    /**
     * 登录接口
     */

    @PostMapping("/login")
    public LoginResponse login(@RequestBody LoginRequest request) {
        return userService.login(request);
    }



    // 必须加 public
    public static class ResponseResult {
        private int code;
        private String msg;

        public ResponseResult(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }
    }
}
